package com.simple.utils;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JsonUtils {
	
	/**
	 * 将回传的json数据转换成需要的格式存储
	 * 如1,2,3
	 * */
	public static String jsonToString(String json) {
		StringBuffer sb=new StringBuffer();
        //需要使用的JSON的parseArray方法，将jsonArray解析为object类型的数组
        JSONArray levelOne = JSON.parseArray(json);
        for(int i=0;i<levelOne.size();i++){
        //通过数组下标取到object，使用强转转为JSONObject，之后进行操作
            JSONObject object = (JSONObject) levelOne.get(i);
            sb.append(object.getString("id")+",") ;
            String levelTwoStr=object.getString("children");
            JSONArray levelTwo = JSONArray.parseArray(levelTwoStr);
            for (int j = 0; j < levelTwo.size(); j++) {
            	 JSONObject levelTwoChild = (JSONObject) levelTwo.get(j);
            	 sb.append(levelTwoChild.getString("id")+",") ;
            	 String levelThreeStr=levelTwoChild.getString("children");
            	 System.out.println(sb);
            	 System.out.println(levelThreeStr);
            	 if(StringUtils.isNotBlank(levelThreeStr)) {
            		 JSONArray levelThree = JSONArray.parseArray(levelThreeStr);
            		 for (int k = 0; k < levelThree.size(); k++) {
            			 JSONObject levelThreeChild = (JSONObject) levelThree.get(k);
            			 sb.append(levelThreeChild.getString("id")+",") ;
					}
            	 }
			}
        }
        //去掉stringbuffer的最后一位的字符
        sb.deleteCharAt(sb.length()-1);
        return sb.toString();
	}
	
	
//	public static void main(String[] args) {
//		//String json="[{\"title\":\"一级1\",\"id\":1,\"field\":\"name1\",\"checked\":true,\"spread\":true,\"children\":[{\"title\":\"二级1-1 可允许跳转\",\"id\":3,\"field\":\"name11\",\"href\":\"https://www.layui.com/\",\"children\":[{\"title\":\"三级1-1-3\",\"id\":23,\"field\":\"\",\"children\":[{\"title\":\"四级1-1-3-1\",\"id\":24,\"field\":\"\",\"children\":[{\"title\":\"五级1-1-3-1-1\",\"id\":30,\"field\":\"\"},{\"title\":\"五级1-1-3-1-2\",\"id\":31,\"field\":\"\"}]}]},{\"title\":\"三级1-1-1\",\"id\":7,\"field\":\"\",\"children\":[{\"title\":\"四级1-1-1-1 可允许跳转\",\"id\":15,\"field\":\"\",\"href\":\"https://www.layui.com/doc/\"}]},{\"title\":\"三级1-1-2\",\"id\":8,\"field\":\"\",\"children\":[{\"title\":\"四级1-1-2-1\",\"id\":32,\"field\":\"\"}]}]},{\"title\":\"二级1-2\",\"id\":4,\"spread\":true,\"children\":[{\"title\":\"三级1-2-2\",\"id\":10,\"field\":\"\"}]},{\"title\":\"二级1-3\",\"id\":20,\"field\":\"\",\"children\":[{\"title\":\"三级1-3-1\",\"id\":21,\"field\":\"\"},{\"title\":\"三级1-3-2\",\"id\":22,\"field\":\"\"}]}]},{\"title\":\"一级2\",\"id\":2,\"field\":\"\",\"spread\":true,\"children\":[{\"title\":\"二级2-2\",\"id\":6,\"field\":\"\",\"children\":[{\"title\":\"三级2-2-1\",\"id\":13,\"field\":\"\"}]}]}]";
//		String json="[{\"menuLevel\":1,\"id\":1,\"title\":\"栏目树\",\"spread\":true,\"children\":[{\"menuLevel\":2,\"id\":2,\"title\":\"权限管理\",\"menuAddress\":\"\",\"children\":[{\"menuLevel\":3,\"id\":3,\"title\":\"用户管理\",\"menuAddress\":\"/user/sysUser.html\"},{\"menuLevel\":3,\"id\":4,\"title\":\"角色管理\",\"menuAddress\":\"/user/sysRole.html\"},{\"menuLevel\":3,\"id\":5,\"title\":\"栏目管理\",\"menuAddress\":\"/user/sysMenu.html\"}]}]}]";
//		System.out.println(jsonToString(json));
//	}

}
